#include "app.h"
#include <uis.h>
#include "../dbg/dbg.h"

// TODO: set previous state here
#define BACK_STATE			MC_STATE_FB

#define STRING_LEN			(FILENAME_MAX_LEN + 1)


// -----------------------------------------------------------------------------------------
// Local constants



// -----------------------------------------------------------------------------------------
// Local functions

static UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app );
static UINT32 HandleData( EVENT_STACK_T * ev_st, void * app );


// -----------------------------------------------------------------------------------------
// Global handlers array

const EVENT_HANDLER_ENTRY_T chared_state_handlers[] =
{
	{ EV_CANCEL,				HandleBack },	// "cancel" soft key
	{ EV_DONE,					HandleDone },	// "ok"
	{ EV_DATA,					HandleData },	// resp. to data req

	{ STATE_HANDLERS_END,		NULL }
};

// -----------------------------------------------------------------------------------------
// Global enter/exit state functions

UINT32 CharEdStateEnter( EVENT_STACK_T * ev_st, void * app, ENTER_STATE_TYPE_T type )
{
	MOTOCMD_T *		papp = motocmd(app);
	SU_PORT_T		port = papp->apt.port;
	UIS_DIALOG_T	dialog;
	
	dbg("CharEdStateEnter: Enter, type = %d", type);
	if ( type != ENTER_STATE_ENTER )
		return RESULT_OK;
	
	// TODO: add your code here
	switch ( papp->cmd ) {
		case CMD_PASTE_AS:
			dialog =
			UIS_CreateCharacterEditor( &port, 
										papp->file_buf.list[0].name,	// text
										0x08,	// type
										STRING_LEN,		// str max length
										FALSE,	// masked, as pw
										NULL,
										LANG_FILENAME );
			break;
		case CMD_FOLDER:
			dialog =
			UIS_CreateCharacterEditor( &port, 
										NULL,	// text
										0x08,	// type
										STRING_LEN,		// str max length
										FALSE,	// masked, as pw
										NULL,
										LANG_FOLDER );
			break;
		default:
			err("CharEdStateEnter: unknown cmd %d", papp->cmd);	// crash
			dialog = 0;
	}
	dbg("CharEdStateEnter: UIS_CreateCharacterEditor DONE, dialog = %d", dialog);
	
	if ( dialog == 0 )
		return RESULT_FAIL;
	papp->apt.dialog = dialog;
	
	dbg("CharEdStateEnter: END", 0);
	return RESULT_OK;
}

UINT32 CharEdStateExit( EVENT_STACK_T * ev_st, void * app, EXIT_STATE_TYPE_T type )
{
	MOTOCMD_T *		papp = motocmd(app);
	UINT32			status;
	
	dbg("CharEdStateExit: Enter, type = %d", type);
	if ( type != EXIT_STATE_EXIT )
		return RESULT_OK;
	
	status = APP_UtilUISDialogDelete( &papp->apt.dialog );
	
	dbg("CharEdStateExit: END, status = %d", status);
	return status;
}


// -----------------------------------------------------------------------------------------
// Other local functions

UINT32 HandleBack( EVENT_STACK_T * ev_st, void * app )
{
//	MOTOCMD_T *		papp = motocmd(app);
	UINT32			status;
	
	dbg("HandleBack: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// TODO: add your code here
	
	status = APP_UtilChangeState( BACK_STATE, ev_st, app );
	dbg("HandleBack: END, status = %d", status);
	return status;
}

UINT32 HandleDone( EVENT_STACK_T * ev_st, void * app )
{
//	MOTOCMD_T *		papp = motocmd(app);
	EVENT_T *		event = AFW_GetEv(ev_st);
	UINT32			status;
	
	dbg("HandleBack: Enter", 0);
	APP_ConsumeEv(ev_st, app);
	
	// Make data (string) reqest
	event->data.index = STRING_LEN;	// how many chars reqest from dialog
	status = AFW_AddEvEvD( ev_st, EV_REQUEST_DATA, (ADD_EVENT_DATA_T *)(&event->data_tag) );
	
	dbg("HandleBack: END, status = %d", status);
	return status;
}

UINT32 HandleData( EVENT_STACK_T * ev_st, void * app )
{
	MOTOCMD_T *		papp = motocmd(app);
	EVENT_T *		event = AFW_GetEv(ev_st);
	WCHAR *			str;
	WCHAR *			str2;
	INT32			err;
	UINT32			status;
	
	dbg("HandleData: Enter", 0);
	APP_ConsumeEv( ev_st, app );
	
	// TODO: save the string
	str = event->attachment;
	
	switch ( papp->cmd ) {
		case CMD_PASTE_AS:
			str2 = suAllocMem(sizeof(WCHAR)*(u_strlen(str) + 1), &err);
			if ( err != RESULT_OK )
				return err;
			u_strcpy(str2, str);
			if ( papp->in_name != NULL ) {
				suFreeMem(papp->in_name);
			}
			papp->in_name = str2;
			// we have new name, now change state
			status = APP_UtilChangeState(MC_STATE_PROGRESS, ev_st, app);
			break;
		case CMD_FOLDER:
			// make a new folder in current dir
			str2 = suAllocMem(sizeof(WCHAR)*(FILEURI_MAX_LEN + 1), &err);
			if ( err != RESULT_OK )
				return err;
			u_mkpath(str2, papp->cur_dir, event->attachment, TRUE);
			status = DL_FsMkDir(str2, FS_PARAM);
			suFreeMem(str2);
			papp->cmd = CMD_REFRESH;
			if ( status == RESULT_OK ) {
				status = APP_UtilChangeState(BACK_STATE, ev_st, app);
			} else {
				errsnd(status != RESULT_OK);
				status = ShowNotice(ev_st, app, LANG_ERROR, NOTICE_TYPE_FAIL, BACK_STATE);
			}
			break;
		default:
			// O_o
			warn("HandleData: unknown cmd %d", papp->cmd);
			status = RESULT_OK;
	}
	
	dbg("HandleData: END", 0);
	return status;
}
